package com.cgsrp.cgsrp_community.service.impl;

import com.cgsrp.cgsrp_community.dao.BlogMapper;
import com.cgsrp.cgsrp_community.service.BlogService;
import com.cgsrp.utils.Result;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.cgsrp.cgsrp_community.dao.BlogLikeMapper;
import com.cgsrp.cgsrp_community.domain.BlogLike;
import com.cgsrp.cgsrp_community.service.BlogLikeService;

import java.util.List;

@Service
public class BlogLikeServiceImpl implements BlogLikeService{

    @Resource
    private BlogLikeMapper blogLikeMapper;

    @Resource
    private BlogService blogService;


    @Override
    public int deleteByPrimaryKey(Integer blogId,Integer userId) {
        return blogLikeMapper.deleteByPrimaryKey(blogId,userId);
    }

    @Override
    public int insert(BlogLike record) {
        return blogLikeMapper.insert(record);
    }

    @Override
    public int insertSelective(BlogLike record) {
        return blogLikeMapper.insertSelective(record);
    }

    @Override
    public Result getLike(int userId) {
        List<Integer> list = blogLikeMapper.findLikeBlogIdByUserId(userId);
        return Result.suc("查询成功",list);
    }

    @Override
    public Result like(BlogLike blogLike) {
        if (blogLikeMapper.insert(blogLike)>0){
            blogService.increaseLevelByLike(blogLike.getBlogId());
            return Result.success("我喜欢");
        }else {
            return Result.failure("系统出错");
        }
    }

    @Override
    public Result dlike(BlogLike blogLike) {
        if (blogLikeMapper.deleteByPrimaryKey(blogLike.getBlogId(),blogLike.getUserId())>0){
            blogService.reduceLevelByLike(blogLike.getBlogId());
            return Result.success("取消点赞");
        }else {
            return Result.failure("系统出错");
        }
    }

}
